package com.yctan.base.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.yctan.base.utils.EncryptionUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.NotBlank;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

;

/**
 * 供应商表
 */
public class Supplier extends BaseEntity {
    private String name;        //供应商名称

    private String address;     //供应商地址

    private String phone;       //电话

    private String fax;         //传真

    private String email;       //邮箱

    private String zipCode;     //邮编

    private String idNumber;   //身份证

    private Date expirationDate;//合同到期时间

    //查询条件，供应商登陆账号
    private String account;

    @NotBlank(message = "供应商名称不能为空")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @NotBlank(message = "供应商地址不能为空")
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    /**
     * 验证电话号码格式
     * @return
     */
    public boolean checkMobilePhoneNum() {
        if(StringUtils.isBlank(phone)){
            return true;
        }else if(phone.contains("*")){
            return true;
        }
        String regex = "^(1[3-9]\\d{9}$)";
        if (phone.length() == 11) {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            if (m.matches()) {
                return true;
            }
        }
        return false;
    }

    public String getFax() {
        return fax;
    }

    public void setFax(String fax) {
        this.fax = fax;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getZipCode() {
        return zipCode;
    }

    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    public String getIdNumber() {
        return idNumber;
    }

    public void setIdNumber(String idNumber) {
        this.idNumber = idNumber;
    }

    public Date getExpirationDate() {
        return expirationDate;
    }

    public void setExpirationDate(Date expirationDate) {
        this.expirationDate = expirationDate;
    }

    @JsonIgnore
    public String getAccount() {
        return account;
    }

    @JsonProperty
    public void setAccount(String account) {
        this.account = account;
    }

    //数据库加密
    public void encryption(){
        if(StringUtils.isBlank(phone)){
            phone = null;

        }else{
            if(phone.contains("*")){
                phone=null;
            }else{
                phone = EncryptionUtils.encryption(phone);
            }
        }
        if(StringUtils.isBlank(email)){
            email = null;

        }else{
            if(email.contains("*")){
                email=null;
            }else{
                email = EncryptionUtils.encryption(email);
            }
        }
        if(StringUtils.isBlank(idNumber)){
            idNumber = null;

        }else{
            if(idNumber.contains("*")){
                idNumber=null;
            }else{
                idNumber = EncryptionUtils.encryption(idNumber);
            }
        }
    }

    //数据库解密
    public void decrypt(){
        if(StringUtils.isNotBlank(idNumber)){
            idNumber = EncryptionUtils.decrypt(idNumber);
        }
        if(StringUtils.isNotBlank(phone)){
            phone = EncryptionUtils.decrypt(phone);
        }
        if(StringUtils.isNotBlank(email)){
            email = EncryptionUtils.decrypt(email);
        }
    }

    //返回前端隐藏
    public void hide(){
        if(StringUtils.isNotBlank(idNumber)){
            idNumber = EncryptionUtils.replaceString2Star(idNumber,5,4);
        }
        if(StringUtils.isNotBlank(phone)){
            phone = EncryptionUtils.replaceString2Star(phone,2,4);
        }
        if(StringUtils.isNotBlank(email)){
            email = EncryptionUtils.replaceString2Star(email,3,3);
        }
    }
}